package com.hungry.hfw.dao;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.RowMapper;


public class HungrySimpleRowMapper implements RowMapper {

	private Class<?> cls;
	private Field[] fields;
	
	
	@Override
	public Object mapRow(ResultSet rs, int arg1) throws SQLException {
		BaseEntity obj = (BaseEntity) BeanUtils.instantiateClass(cls);
		
		fields = cls.getDeclaredFields();
		int columnCount = 0;
		for(Field f : fields) {
			try {

				f.setAccessible(true);
				int j = f.getModifiers();
				if(j!=2 && j!= 1){
					
				}
				else {
					columnCount++;
					System.out.println("value - " + rs.getObject(f.getName()).getClass());
					f.set(obj, rs.getObject(f.getName()));
				}
				System.out.println(j + "-" +  f.getName());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}
	public void setCls(Class cls) {
		this.cls = cls;
	}
	public Class getCls() {
		return cls;
	}

	public static void main(String[] args) throws Exception {
		
		HungrySimpleRowMapper hs = new HungrySimpleRowMapper();
		hs.setCls(SimpleEntity.class);
		hs.mapRow(null, 1);
//		for(int i=0;i <  40; i++) { 
//			System.out.println(i + "-" + Modifier.toString(i));
//		}
	}
	
	
	
	
}
